package com.huanglj.himalaya.presenters;

import androidx.annotation.NonNull;

import com.huanglj.himalaya.base.BaseApplication;
import com.huanglj.himalaya.data.dao.HistoryDao;
import com.huanglj.himalaya.data.dao.IHistoryDaoCallBack;
import com.huanglj.himalaya.interfaces.IHistoryCallBack;
import com.huanglj.himalaya.interfaces.IHistoryPresenter;
import com.huanglj.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

public class HistoryPresenter implements IHistoryPresenter, IHistoryDaoCallBack {

    private static final String TAG = "HistoryPresenter";
    private static HistoryPresenter sInstance = null;
    private final HistoryDao mHistoryDao;
    private List<IHistoryCallBack> mCallBack = new ArrayList<>();
    private List<Track> mTracks = new ArrayList<>();
    private Track mTrack;

    public HistoryPresenter(){
        mHistoryDao = HistoryDao.getInstance();
        mHistoryDao.registerViewCallBack(this);
    }

    public static HistoryPresenter getInstance(){
        if (sInstance == null) {
            synchronized (HistoryPresenter.class){
                if (sInstance == null) {
                    sInstance = new HistoryPresenter();
                }
            }
        }
        return  sInstance;
    }
    @Override
    public void addHistory(final Track track) {
        if (mTracks.size() >= 100) {
            isDoDelAsOutOfSize = true;
            mTrack = track;
            deleteHistory(mTracks.get(mTracks.size() - 1));
        }else {
            doAddHistory(track);
        }
    }

    private void doAddHistory(final Track track) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Object> emitter) {
                mHistoryDao.addTracks(track);
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void deleteHistory(final Track track) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Object> emitter) {
                mHistoryDao.deleteTracks(track);
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void clearHistory() {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Object> emitter) {
                mHistoryDao.clearHistory();
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void getHistoryList() {
        getList();
    }

    public void getList() {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Object> emitter) {
                //只调用，不处理结果
                mHistoryDao.getHistoryList();
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void unRegisterViewCallBack(IHistoryCallBack iHistoryCallBack) {
        mCallBack.remove(iHistoryCallBack);
    }

    @Override
    public void registerViewCallBack(IHistoryCallBack iHistoryCallBack) {
        if (! mCallBack.contains(iHistoryCallBack)) {
            mCallBack.add(iHistoryCallBack);
        }
    }

    private boolean isDoDelAsOutOfSize = false;
    @Override
    public void onHistoryAdd(final boolean success) {
        getList();
    }

    @Override
    public void onHistoryDelete(final boolean success) {
        if (isDoDelAsOutOfSize && mTrack != null) {
            isDoDelAsOutOfSize = false;
            doAddHistory(mTrack);
        }else {
            getList();
        }
    }

    @Override
    public void onHistoryClear() {
        BaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                for (IHistoryCallBack callBack : mCallBack) {
                    callBack.onHistoryClear();
                }
            }
        });
    }

    @Override
    public void onHistoryList(final List<Track> historyList) {
        LogUtil.d(TAG,"history: " + historyList.size());
        mTracks.addAll(historyList);
        BaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                for (IHistoryCallBack callBack : mCallBack) {
                    callBack.onHistoryList(historyList);
                }

            }
        });
    }
}
